package com.qc.controller.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qc.common.StringUtil;
import com.qc.entity.SecScheduleEntityVo;
import com.qc.entity.SystemMessageEntity;
import com.qc.jpush.JPushUtil;
import com.qc.pageUtil.page.page.Page;
import com.qc.pageUtil.page.page.PageModel;
import com.qc.pageUtil.page.page.PageModelList;
import com.qc.pageUtil.page.vo.NewsAndNav;
import com.qc.service.SystemMessageService;
import com.qc.service.UserSecScheduleService;

@SuppressWarnings({ "rawtypes", "unchecked" })
@Controller
@RequestMapping("/web")
public class WebUserSecScheduleController {

    @Resource(name="userSecScheduleService")
    private UserSecScheduleService userSecScheduleService;
    
    @Resource(name="systemMessageService")
	private SystemMessageService systemMessageService;
    
    /**
     * 秘书发布首页
     * @return
     */
    @RequestMapping(value = "/toSecSchedule", produces = "text/html;charset=UTF-8")
    public ModelAndView toSecSchedule() {
        ModelAndView mav = new ModelAndView("/sec/sec_fabu");
        return mav;
    }
    
    /**
     * 秘书评论管理
     * @return
     */
    @RequestMapping(value = "/toSecScheduleComm", produces = "text/html;charset=UTF-8")
    public ModelAndView toSecScheduleComm() {
        ModelAndView mav = new ModelAndView("/sec/sec_pinglun");
        return mav;
    }
    
    /**
     * 所有秘书发布数据展示
     * @param request
     * @return
     */
    @RequestMapping(value = "/toListAllSecSchedule", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String toListAllSecSchedule(HttpServletRequest request) {
        
        String userId = request.getParameter("userId");
        String createTime = request.getParameter("createTime");
        //当前页面
        int pageNo = Integer.parseInt(request.getParameter("pageNo"));
        //每页显示多少条
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        
        //根据条件总数
        int recordCount = userSecScheduleService.getListUserSecScheduleCount(userId, createTime);
        
        //根据条件查询当前页数据
        List<SecScheduleEntityVo> list = userSecScheduleService.getAllUserSecSchedule(userId, createTime, pageNo, pageSize);
        
        //分页查询公共类
        Page<SecScheduleEntityVo> page = new Page<SecScheduleEntityVo>(pageNo, recordCount,pageSize);
        page.setList(list);//设置结果集  
        PageModelList<SecScheduleEntityVo> pageModel = new PageModelList<SecScheduleEntityVo>(page, "#", PageModel.NUM_MODEL,"doPage","1");//设置页码显示类型  
        //4 组装带翻页导航功能的对象
        NewsAndNav objs = new NewsAndNav(list, pageModel.getPageModel());
        
        return JSONObject.fromObject(objs).toString();
        
    }
    
    /**
     * 删除发布
     */
    @RequestMapping(value = "/toDeleteSecScheduleId")
    @ResponseBody
    public int toDeleteSecScheduleId(HttpServletRequest request) {
        long id = Long.parseLong(request.getParameter("id"));
        
        SecScheduleEntityVo entityVo = userSecScheduleService.getSecScheduleId(id);
        
        SystemMessageEntity messageEntity = new SystemMessageEntity();
        
        if (StringUtil.isNotEmpty(entityVo.getTitle())) {
        	messageEntity.setUserId(entityVo.getUserId());
        	messageEntity.setContent("您好，您的《"+entityVo.getTitle()+"》分享生活存在违规，已被系统自动删除~");
        	messageEntity.setSystemType(1);
        	
        	systemMessageService.saveSystemMessage(messageEntity);
        	
        	JPushUtil.redDotPush(7, entityVo.getUserId(), "您好，您的《"+entityVo.getTitle()+"》分享生活存在违规，已被系统自动删除~");
        }
        
        int flag = userSecScheduleService.deleteUserSecScheduleId(id);
        return flag;
    }
    
    /**
     * 查看详细页面
     * @return
     */
    @RequestMapping(value = "/toSecChakan")
    public ModelAndView toSecChakan(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("/sec/sec_fabuchakan");
        
        long id = Long.parseLong(request.getParameter("id"));
        
        SecScheduleEntityVo scheduleEntityVo = userSecScheduleService.getSecScheduleId(id);
        
        String[] b = scheduleEntityVo.getSsContent().split("~qc~");
        String content = "";
        for (String c : b) {
            String[] d = c.split("&qc&");
            
            for (int i = 0 ; i<d.length;i++) {
            	if (i == 0) {
            		content += "<p class=\"font3\">"+d[i]+"</p>";
            	}else {
            		
            		String e = "<img src=http://qiniu.images.69mark.com/"+d[i]+">";
                    content += e;
                    
            		/* String name = "http://qiniu.images.69mark.com/"+d[i];
                     
                	 eImage = "<img style='width: 100%;' src="+name+">";
                	 content += eImage;*/
            	}
            }
            
            /*if (d.length>1) {
            	if (d[0] != null) {
                    content += "<p class=\"font3\">"+d[0]+"</p>";
                }
                
                if (d[1] != null) {
                    String e = "<img src=http://qiniu.images.69mark.com/"+d[1]+">";
                    content += e;
                }
            }else {
            	if (d[0] != null) {
                    content += "<p class=\"font3\">"+d[0]+"</p>";
                }
            }*/
        }
        scheduleEntityVo.setSsContent(content);
        mav.addObject("scheduleEntityVo",scheduleEntityVo);
        
        return mav;
    }
    
    
    /**
     * 评论管理
     */
    @RequestMapping(value = "/toListAllSecScheduleComm", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String toListAllSecScheduleComm(HttpServletRequest request) {
        
        String name = request.getParameter("name");
        //当前页面
        int pageNo = Integer.parseInt(request.getParameter("pageNo"));
        //每页显示多少条
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        
        //根据条件总数
        int recordCount = userSecScheduleService.getListSecScheduleCommCount(name);
        
        //根据条件查询当前页数据
        List<SecScheduleEntityVo> list = userSecScheduleService.getAllSecScheduleComm(name, pageNo, pageSize);
        
        //分页查询公共类
        Page<SecScheduleEntityVo> page = new Page<SecScheduleEntityVo>(pageNo, recordCount,pageSize);
        page.setList(list);//设置结果集  
        PageModelList<SecScheduleEntityVo> pageModel = new PageModelList<SecScheduleEntityVo>(page, "#", PageModel.NUM_MODEL,"doPage","1");//设置页码显示类型  
        //4 组装带翻页导航功能的对象
        NewsAndNav objs = new NewsAndNav(list, pageModel.getPageModel());
        
        return JSONObject.fromObject(objs).toString();
        
    }
    
    /**
     * 评论 删除
     * @param request
     * @return
     */
    @RequestMapping(value = "/toDeleteSecScheduleCommId")
    @ResponseBody
    public int toDeleteSecScheduleCommId(HttpServletRequest request) {
        long id = Long.parseLong(request.getParameter("id"));
        
        int flag = userSecScheduleService.deleteSecScheduleCommId(id);
        return flag;
    }
}
